import { createApp } from 'vue';
import App from './App.vue';
const app = createApp(App);
//轮询自定义刷新
import './utils/auto-update.ts';
//自定义样式
import '@/styles/index.scss';
import '@/font/iconfont.css';
//路由
import router from './router/index.ts';
app.use(router);
//引入Element Plus
import ElementPlus from 'element-plus';
import 'element-plus/dist/index.css';
import zhCn from 'element-plus/dist/locale/zh-cn.mjs';
app.use(ElementPlus, {
  locale: zhCn,
  zIndex: 10,
});
//json可视化
import JsonViewer from 'vue3-json-viewer';
//json可视化添加样式
import 'vue3-json-viewer/dist/index.css';
app.use(JsonViewer);
//api接口$
import api from './api/index.ts';
app.config.globalProperties.$api = api;
//国际语音
import { i18n } from '@/language/index.ts';
app.use(i18n);
// 注册所有图标
import * as ElementPlusIconsVue from '@element-plus/icons-vue';
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
//通用方法
import com from './utils/com.ts';
app.config.globalProperties.$com = com;
// 引入 moment
import moment from 'moment';
app.config.globalProperties.$moment = moment;
//计算
import { create, all } from 'mathjs';
// 配置 math.js 使用 Decimal 类型
const math = create(all, {
  number: 'BigNumber', // 使用 BigNumber 类型
});
app.config.globalProperties.$math = math;
// 引入中文语言环境
import 'moment/dist/locale/zh-cn';
// 设置全局使用中文
moment.locale('zh-cn');
//

// 将moment挂载到Vue原型上
//下载
import { downloadByBlob, downloadImg, downloadFile } from './utils/downloadByBlob.ts';
//下载图片
app.config.globalProperties.$downloadByBlob = downloadByBlob;
app.config.globalProperties.$downloadImg = downloadImg;
app.config.globalProperties.$downloadFile = downloadFile;

//计算
import { count } from './utils/mathjs.ts';
app.config.globalProperties.$count = count;
//弹出
import { openPup, closePup } from './utils/pup.ts';
app.config.globalProperties.$openPup = openPup; //打开弹窗
app.config.globalProperties.$closePup = closePup; //关闭弹窗
//颜色
import { hexAlpha } from './utils/color.ts';
app.config.globalProperties.$hexAlpha = hexAlpha; //hex颜色透明度
//模版
import { DownLoadTemp } from './utils/downLoadTemplate.ts';
app.config.globalProperties.$DownLoadTemp = DownLoadTemp; //hex颜色透明度
//物料
import { getGood, getProduce } from './utils/getApi.ts';
app.config.globalProperties.$getGood = getGood; //获取物料信息
app.config.globalProperties.$getProduce = getProduce; //获取物料信息

//权限
import { permission } from './utils/permission.ts';
app.config.globalProperties.$permission = permission;
//税率
import { setTaxRate, setTaxExcludedUnitPrice, setTaxInclusiveUnitPrice } from './utils/tax.ts';
app.config.globalProperties.$setTaxRate = setTaxRate; //修改税率
app.config.globalProperties.$setTaxExcludedUnitPrice = setTaxExcludedUnitPrice; //修改-未税单价
app.config.globalProperties.$setTaxInclusiveUnitPrice = setTaxInclusiveUnitPrice; //修改-含税单价
//通用转换
import {
  mmToPx,
  size,
  chineseNumber,
  highlightedText,
  message,
  messageBox,
  messageInputBox,
  money,
  isFunction,
  copy,
  stripHtmlTags,
  decodeHtmlEntities,
  toUpperCase,
  toLowerCase,
  toPascalCase,
  toCamelCase,
  toLocaleString,
  arrTotal,
  arrFind,
  isArr,
  ObjToObj,
  getNewObj,
  clickOrDoubleClick,
  downloadUrl,
  previewContent,
  deepHasValue,
  sortArr,
  hasMultipleMatches,
  initializeAtPath,
  openUrl,
  getEvenColumnCount,
  useResize,
  flattenObjectArray,
  filterArr,
} from './utils/componen.ts';
app.config.globalProperties.$isArr = isArr; //是否数组
app.config.globalProperties.$mmToPx = mmToPx; //毫米转px
app.config.globalProperties.$size = size; //尺寸
app.config.globalProperties.$chineseNumber = chineseNumber; //数字转大写
app.config.globalProperties.$highlightedText = highlightedText; //文本高亮
app.config.globalProperties.$message = message; //消息提示
app.config.globalProperties.$messageBox = messageBox; //消息询问
app.config.globalProperties.$messageInputBox = messageInputBox; //消息询问+原因
app.config.globalProperties.$money = money; //金钱格式
app.config.globalProperties.$isFunction = isFunction; //是否是方法
app.config.globalProperties.$copy = copy; //复制
app.config.globalProperties.$stripHtmlTags = stripHtmlTags; //清除html标签信息
app.config.globalProperties.$decodeHtmlEntities = decodeHtmlEntities; //还原信息
app.config.globalProperties.$toUpperCase = toUpperCase; //英文字母全部大写
app.config.globalProperties.$toLowerCase = toLowerCase; //英文字母全部小写
app.config.globalProperties.$toCamelCase = toCamelCase; //英文字母小驼峰
app.config.globalProperties.$toPascalCase = toPascalCase; //英文字母打驼峰
app.config.globalProperties.$toLocaleString = toLocaleString; //千位分隔符
app.config.globalProperties.$arrTotal = arrTotal; //数组求和
app.config.globalProperties.$arrFind = arrFind; //关键组件
app.config.globalProperties.$ObjToObj = ObjToObj; //新老数据转换
app.config.globalProperties.$getNewObj = getNewObj; //获取新数据
app.config.globalProperties.$clickOrDoubleClick = clickOrDoubleClick; //点击/双击判断逻辑
app.config.globalProperties.$downloadUrl = downloadUrl; //下载文件
app.config.globalProperties.$previewContent = previewContent; //预览文件
app.config.globalProperties.$deepHasValue = deepHasValue; //判断嵌套对象中是否存在某个值（深度查找）
app.config.globalProperties.$sortArr = sortArr; //排序
app.config.globalProperties.$hasMultipleMatches = hasMultipleMatches; //判断数组是否有多条满足
app.config.globalProperties.$initializeAtPath = initializeAtPath; //判断数组是否有多条满足
app.config.globalProperties.$openUrl = openUrl; //打开地址
app.config.globalProperties.$getEvenColumnCount = getEvenColumnCount; //计算最多可以显示多少列，并且返回的列数要是 2 的倍数
app.config.globalProperties.$useResize = useResize; //窗口防抖（debounce）
app.config.globalProperties.$flattenObjectArray = flattenObjectArray; //对象数组的平铺（扁平化）
app.config.globalProperties.$filterArr = filterArr; //过滤新的数组
//导出Excel表
import { exportData } from './utils/export.ts';
app.config.globalProperties.$exportData = exportData;
//时间相关
import {
  time,
  day,
  formatDate,
  compareDates,
  getMinutesDifference,
  getTimeAgo,
  getDayName,
  getRelativeTime,
  addMinutesToTime,
  getDaysUntilDeadline,
  getYearDifference,
  isBeyondOneMonth,
} from './utils/moment.ts';
app.config.globalProperties.$time = time; ///格式化时间
app.config.globalProperties.$day = day; //格式化日期
app.config.globalProperties.$formatDate = formatDate; //格式化时间为指定格式，默认为 YYYY-MM-DD
app.config.globalProperties.$compareDates = compareDates; //比较两个时间的先后关系
app.config.globalProperties.$getMinutesDifference = getMinutesDifference; //获取两个时间之间的分钟差
app.config.globalProperties.$getTimeAgo = getTimeAgo; //获取某个时间段前的日期
app.config.globalProperties.$getDayName = getDayName; //获取星期几名称
app.config.globalProperties.$getRelativeTime = getRelativeTime; //获取相对时间
app.config.globalProperties.$addMinutesToTime = addMinutesToTime; //获取某个时间加上若干分钟后的新时间
app.config.globalProperties.$getDaysUntilDeadline = getDaysUntilDeadline; //计算与目标日期相差的天数
app.config.globalProperties.$getYearDifference = getYearDifference; //获取两个日期之间的年份差
app.config.globalProperties.$isBeyondOneMonth = isBeyondOneMonth; //判断两个日期之间的间隔是否超过N个月
//缓存
import { setItem, getItem, removeItem } from './utils/local.ts';
app.config.globalProperties.$setItem = setItem; ///格式化时间
app.config.globalProperties.$getItem = getItem; //格式化日期
app.config.globalProperties.$removeItem = removeItem; //格式化时间为指定格式，默认为 YYYY-MM-DD
//pinia
import { createPinia } from 'pinia';
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate';
const pinia = createPinia();
pinia.use(piniaPluginPersistedstate); //
app.use(pinia);
//常用数组
import { optionsArr } from './utils/optionsArr.ts';
app.config.globalProperties.$optionsArr = optionsArr;
//自定义指令
// 自定义一个全局指令 focus, 作用是让其 dom 获得焦点
//获取pinia
import { useUserStore } from '@/store/index';
const userStores: any = useUserStore();
app.config.globalProperties.$user = userStores;

// 注册 v-print 指令
import Print from 'vue3-print-nb';
app.directive('print', Print);
app.directive('permission', {
  // 当被绑定的元素插入到 DOM 中时。
  mounted(el, binding) {
    checkPermission(el, binding);
  },

  // 在包含组件的 VNode 及其子组件的 VNode 更新后调用
  updated(el, binding) {
    checkPermission(el, binding);
  },
});

const checkPermission = (el: any, binding: any) => {
  const requiredPermission = binding.value;
  if (!requiredPermission) return;
  setTimeout(() => {
    const userPermissions = userStores.buttons || []; // 假设这是获取用户权限的方法
    const hasPermission = Array.isArray(userPermissions) && userPermissions.includes(requiredPermission);
    if (!hasPermission) {
      // 没有权限，隐藏元素而非直接移除
      el.style.display = 'none';
    } else {
      // 有权限，显示元素
      el.style.display = '';
    }
  }, 100);
};
//挂载
app.mount('#app');
